package com.kkb.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/servlet6")
public class ContextServlet6 extends HttpServlet {
    private ServletContext context;

    @Override
    public void init() throws ServletException {
        this.context = super.getServletContext();
        this.context.setAttribute("count", 0);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int count = (int) this.context.getAttribute("count");
        count++;
        this.context.setAttribute("count", count);
        response.getWriter().print(count);
    }
}
